package com.letter.otools.day;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import android.content.DialogInterface;
import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.letter.otools.day.R;
import com.letter.otools.day.admob.AdmobBannerActivity;
import com.letter.otools.day.util.AppUtils;
import com.letter.otools.day.util.StringUtils;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnniversaryActivity extends AdmobBannerActivity {

    private ActionBar actionBar;
    private Anniversary anniversary;

    private TextView anniDate;
    private TextView anniDays;
    private View anniDaysRoot;
    private TextView anniText;
    private TextView anniType;
    private TextView anni_content;

    private int anniId;
    private int isEdited = 0;
    private FrameLayout adViewContainer;

    @Override
    protected void initBinder() {
        super.initBinder();
        adViewContainer = findViewById(R.id.ad_view_container);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_anniversary);
        Toolbar toolbar = findViewById(R.id.Toolbar);
        setSupportActionBar(toolbar);
//        this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(AppUtils.getNightMode(this) == MODE_NIGHT_YES ? R.drawable.ic_toolbar_back_white_24dp : R.drawable.ic_toolbar_back_gray_24dp);
            actionBar.setTitle("");
        }

        anniId = getIntent().getIntExtra("anniId", 0);
        anniversary = LitePal.find(Anniversary.class, anniId);

        if (anniversary == null) {
            Toast.makeText(AnniversaryActivity.this, "发生错误", Toast.LENGTH_SHORT).show();
        } else {
            anniversary = new Anniversary();
        }

        anniText = findViewById(R.id.anni_text);
        anniDate = findViewById(R.id.anni_date);
        anniDays = findViewById(R.id.anni_days);
        anniDaysRoot = findViewById(R.id.anni_days_root);
        anniType = findViewById(R.id.anni_type);
        anni_content = findViewById(R.id.anni_content);
        freshData();
//        Animation animation = AnimationUtils.loadAnimation(this, R.anim.srb_scale_up_down);
        Animation animation = new ScaleAnimation(1.0f, 0.95f, 1.0f, 0.95f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(2000);
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.REVERSE); // 每次循环从头开始播放
        anniDaysRoot.startAnimation(animation);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_anniversary;
    }

    @Override
    protected FrameLayout getAdContainer() {
        return adViewContainer;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.anniversary_toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            if (isEdited == 1) {
                intent = new Intent();
                setResult(RESULT_OK, intent);
            }
            finish();
        } else if (itemId == R.id.delete) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(AnniversaryActivity.this);
            dialog.setMessage("Confirm to delete this anniversary？");
            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    LitePal.delete(Anniversary.class, anniversary.getId());
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
//                        Intent broadIntent  = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
//                        getApplicationContext().sendBroadcast(broadIntent);
                    Toast.makeText(AnniversaryActivity.this, "Removed!", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            dialog.show();
        } else if (itemId == R.id.edit) {
            intent = new Intent(AnniversaryActivity.this, AddItemActivity.class);
            intent.putExtra("editType", AddItemActivity.ITEM_EDIT);
            intent.putExtra("anniId", anniversary.getId());
            startActivityForResult(intent, 1);
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        if (isEdited == 1) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    isEdited = 1;
                    freshData();
                }
                break;

            default:
                break;
        }
    }


    public static final String NumberString_Regex = "\\d+";

    private void freshData() {
        anniversary = LitePal.find(Anniversary.class, anniId);
        anniText.setText(anniversary.getText());
        anniDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(anniversary.getTime()));
        anniDays.setText(anniversary.getDaysText());
        anniType.setText(anniversary.getTypeText());
        anni_content.setText(anniversary.getContent());

        String text = anniversary.getDaysText();
        Pattern pattern = Pattern.compile(NumberString_Regex);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            System.out.println(matcher.groupCount());
            StringUtils.setColorSpan(anniDays, text, matcher.group(0), getResources().getColor(R.color.textColorItemName));
        }
    }
}
